package com.ist.smartstation;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class Map extends MapActivity {
	
	private MapView mapView;
	private MapController mapController;
	private GeoPoint geoPoint;
	
	public float analyseScreen() {
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		return metric.densityDpi;
	}
	
    class MapOverlay extends Overlay {
    	 
    	@Override
    	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
            Point screenPts = new Point();
            mapView.getProjection().toPixels(geoPoint, screenPts);
            int diff = 62;
            float density = analyseScreen();
            if(density == DisplayMetrics.DENSITY_HIGH){
            	diff = 62;
            }
            if(density == DisplayMetrics.DENSITY_MEDIUM){
            	diff = 38;
            }
            if(density == DisplayMetrics.DENSITY_MEDIUM){
            	diff = 26;
            }
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.red_pin);            
            canvas.drawBitmap(bmp, screenPts.x-10, screenPts.y-diff, null);  
    		super.draw(canvas, mapView, shadow);
        }
    	
    	@Override
    	public boolean onTouchEvent(MotionEvent e, MapView mapView) {
            if (e.getAction() == 1) {                
                geoPoint = mapView.getProjection().fromPixels(
                    (int) e.getX(),
                    (int) e.getY());
                Toast.makeText(getBaseContext(), 
                		geoPoint.getLatitudeE6() / 1E6 + "," + 
                		geoPoint.getLongitudeE6() /1E6 , 
                        Toast.LENGTH_SHORT).show();
            }
            return false;
    	}
    	
    }
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        initMap();
        initOverlays();
//        myLocation();
        Button confirmButton = (Button) findViewById(R.id.confirm_location);
        confirmButton.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
	    		finish();
			}
		});
    }

	private void initMap(){
    	mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapController = mapView.getController();
    }
	
	private void initOverlays() {
        geoPoint = new GeoPoint(38736779, -9138565);
        MapOverlay mapOverlay = new MapOverlay();
        mapView.getOverlays().add(mapOverlay);
//      38736779 -9138565
        mapController.animateTo(geoPoint);
        mapController.setZoom(15); 
		mapView.invalidate();
	}
	
	private void myLocation() {
		MyLocationOverlay myLocOverlay = new MyLocationOverlay(this, mapView);
		myLocOverlay.enableMyLocation();
		mapView.getOverlays().add(myLocOverlay);
		mapView.getController().animateTo(myLocOverlay.getMyLocation());
		mapView.postInvalidate();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}